DIR=$(cd "$(dirname "$0")";pwd)
root="${HOME}/temp/install"
while getopts ":n:p:r:P:" opt
do
    case $opt in
        P)
        pythonl=$OPTARG
        echo -e "\033[40;37m 您输入的python版本$OPTARG\033[0m"
        ;;
        r)
        root=$OPTARG
        echo -e "\033[40;37m 您输入的暴露脚本$OPTARG\033[0m"
        ;;
        n)
        name=$OPTARG
        echo -e "\033[40;37m 您输入的应用名称$OPTARG\033[0m"
        ;;
        p)
        path=$OPTARG
        echo -e "\033[40;37m 您输入的应用路径:$OPTARG\033[0m"
        ;;
        ?)
        echo -e "\033[40;37m 未知参数\033[0m"
        exit 1;;
    esac
done
random=$((RANDOM%100+1))
random=$(printf $random | md5sum | cut -d ' ' -f1)
pre_path="6a/7e/4f"
if [[ ${HOME}/temp/$pre_path/${random} ]]
then
  mkdir -p ${HOME}/temp/${pre_path}/${random}
fi
real_sh_random_md5_name="encry_"$(printf $random | md5sum | cut -d ' ' -f1)
compiler_pre_path=${HOME}/temp/${pre_path}/${random}
cp ${DIR}/setup.py ${compiler_pre_path}/
cp ${DIR}/process.py ${compiler_pre_path}/${real_sh_random_md5_name}.py
cp ${DIR}/install.sh ${compiler_pre_path}/
cp ${DIR}/expose.sh ${compiler_pre_path}/
first_name=${random:0:2}
second_name=${random:2:4}
third_name=${random:4:6}
real_sc_path=/home/yapai/.temp/${first_name}/$second_name/$third_name
sc_content=$(cat ${path} | base64 | sed ':a;N;$!ba;s/\n//g' )
sed -i 's/{{appName}}/'"${name}"'/g' ${compiler_pre_path}/setup.py
sed -i 's:{{appPath}}:'"${real_sh_random_md5_name}"'.py:g' ${compiler_pre_path}/setup.py
sed -i 's#{{script}}#'"${sc_content}"'#g' ${compiler_pre_path}/${real_sh_random_md5_name}.py
sed -i 's:{{path}}:'"${real_sh_random_md5_name}"':g' ${compiler_pre_path}/install.sh
sed -i 's:{{target}}:'"${real_sc_path}"':g' ${compiler_pre_path}/install.sh
sed -i 's:{{moduleName}}:'"${real_sh_random_md5_name}"':g' ${compiler_pre_path}/expose.sh
sed -i 's:{{pythonl}}:'"${pythonl}"':g' ${compiler_pre_path}/expose.sh
leave_count=$(ls ${HOME}/temp/${pre_path}/${random} | wc -l)
cd $compiler_pre_path
shc -v -r -f expose.sh
mv expose.sh.x expose
cp ${path} ${real_sh_random_md5_name}
${pythonl} setup.py build_ext --inplace
file_name=${real_sh_random_md5_name}
so_file=$(ls -1 | grep -E  "${file_name}.*\.so")
tar -zcvf ${root}/${name}.tar.gz  ${so_file}  expose
# if [[ "$leave_count" -eq 0 ]]
# then
#   rm -rf ${HOME}/temp/${pre_path}/${random}
# fi
